package com.rongc.client.freight.business.mine.view.activity;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.exception.RegexException;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.core.utils.RegexUtil;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.request.result.NullResult;
import com.rongc.client.freight.business.mine.request.api.UpdateNickApi;
import com.rongc.client.freight.utils.UtilMethod;

import butterknife.Bind;

/**
 * Created by ccqx on 2017/7/20.
 */

public class EditActivity extends BaseActivity {
    @Bind(R.id.et_content)
    EditText mEtContent;

    Response.Listener<NullResult> respOrderListener = new Response.Listener<NullResult>() {

        @Override
        public void onResponse(NullResult response) {
            if (HandleCode.requestSuccess()) {
                UniApplication.getInstance().getUserInfo().setNickname(mEtContent.getText().toString());
                UniApplication.getInstance().getUserInfo().save();
                setResult(RESULT_OK);
                finish();
            } else {
                UtilMethod.dismissProgressDialog(EditActivity.this);
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
            UtilMethod.dismissProgressDialog(EditActivity.this);
        }
    };



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.map_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_define:
                try{
                    prepare();

                    RequestManager.getInstance().call(new UpdateNickApi(
                            new UpdateNickApi.UpdateNickParams(mEtContent.getText().toString()),
                            respOrderListener, errorListener));
                }catch (RegexException e){
                    ActivityUtils.toast(e.getMessage());
                }

                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    void prepare() throws RegexException {
        if(!RegexUtil.checkNotNull(mEtContent.getText().toString())){
            throw new RegexException("请输入昵称");
        }
    }

    @Override
    public void initView() {
        initToolbar(R.string.setting_nickname_lable);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_edit;
    }
}
